package com.mine.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mine.dao.CartMapper;
import com.mine.dao.GoodsMapper;
import com.mine.pojo.Cart;
import com.mine.pojo.CartDataInfo;
import com.mine.pojo.GoodsDataInfo;
import com.mine.pojo.MineUser;
import com.mine.result.CodeMsg;
import com.mine.result.Result;
import com.sun.tools.classfile.Opcode.Set;

@Service
public class CartServiceImpl implements CartService {

	@Autowired
	CartMapper cartMapper;
	
	@Autowired
	GoodsMapper gMapper;
	
	@Autowired
	UserService userService;
	
	@Override
	public Result addOneGoodsToCart(String token, Cart cart) {
		//先查询用户信息
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		cart.setUserId(user.getId());
		//补全商品数据，根据商品id查询该商品信息
		//GoodsDataInfo goodsDetailById = gMapper.getGoodsDetailById(cart.getGoodsId());
//		cart.setShopName(goodsDetailById.gets);
		//先查询之前购物车中是否有该商品
		List<Cart> list = cartMapper.selectOneGoodsOnCart(cart);
		if (list==null||list.size()==0) {//之前没有，则添加
			//System.out.println("之前没有，则添加");
			cartMapper.addOneGoodsToCart(cart);
			return Result.success(cart);
		}
		
		return Result.ok();
	}

//	@Override
//	public Result deleteOneGoodsOnCart(String token, Cart cart) {
//		//先查询用户信息
//		MineUser user = userService.getRedisUserByToken(token);
//		if (user==null) {
//			return Result.error(CodeMsg.SESSION_ERROR);
//		}
//		cart.setUserId(user.getId());
//		cartMapper.deleteOneGoodsOnCart(cart);
//		return Result.ok();
//	}
//
//	@Override
//	public Result selectOneGoodsOnCart(String token, Cart cart) {
//		//先查询用户信息
//		MineUser user = userService.getRedisUserByToken(token);
//		if (user==null) {
//			return Result.error(CodeMsg.SESSION_ERROR);
//		}
//		cart.setUserId(user.getId());
//		List<Cart> list = cartMapper.selectOneGoodsOnCart(cart);
//		return Result.success(list);
//	}

	@Override
	public Result updateOneGoodsOnCart(String token, Cart cart) {
		//先查询用户信息
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		cart.setUserId(user.getId());
		System.out.println("购物车数据"+cart.toString());
		//查询购物车中是否已经添加了该商品
		List<Cart> list = cartMapper.selectOneGoodsOnCart(cart);
		if (list==null||list.size()==0) {//之前没有，则添加
			//System.out.println("之前没有，则添加");
			cart.setCreateTime(new Date());
			cartMapper.addOneGoodsToCart(cart);
			return Result.success(cart);
		}
		Cart oldCart = list.get(0);
		cart.setId(oldCart.getId());
		//表示更新
		//System.out.println("----更新购物车数据----");
		if(cart.getId()!=null){//有主键 可以先根据主键更新
			System.out.println("----根据主键更新----");
			if (cart.getChooseNum()==0) {//当设置为0 删除这条数据
				System.out.println("----根据主键删除这条数据----");
				cartMapper.deleteOneGoodsOnCartById(cart);
				return Result.ok();
			}
			System.out.println("----根据主键更新这条数据----");
			if(cart.getFlag()!=null&&cart.getFlag()==1){
				//cart.getFlag()==1 先查出购物车某个商品之前的数据，然后加上现在的chooseNum。flag==0表示chooseNum直接替换之前的购物车数据
				oldCart = list.get(0);
				Integer oldNum = oldCart.getChooseNum();
				Integer newNum = oldNum + cart.getChooseNum();
				cart.setChooseNum(newNum);
				cartMapper.updateOneGoodsOnCartById(cart);
			}else{
				cartMapper.updateOneGoodsOnCartById(cart);
			}
			
				//System.out.println("更新了购物车数据1"+cart.toString());
		}else{
			if (cart.getChooseNum()==0) {//当设置为0 删除这条数据
				System.out.println("----根据商品id删除这条数据----");
				cartMapper.deleteOneGoodsOnCartByUserIdAndGoodsId(cart);
					return Result.ok();
				}
				//System.out.println("更新了购物车数据");
				//System.out.println("更新了购物车数据"+cart.toString());
				System.out.println("----根据商品id更新----");
				if(cart.getFlag()!=null&&cart.getFlag()==1){
					oldCart = list.get(0);
					Integer oldNum = oldCart.getChooseNum();
					Integer newNum = oldNum + cart.getChooseNum();
					cart.setChooseNum(newNum);
					cartMapper.updateOneGoodsOnCartById(cart);
				}else{
					cartMapper.updateOneGoodsOnCartByUserIdAndGoodsId(cart);
				}
				
		}
		System.out.println("更新后的数据"+cart.toString());
		return Result.success(cart);

	}

	@Override
	public Result selectCartList(String token) {
		//先查询用户信息
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		Cart cart = new Cart();
		cart.setUserId(user.getId());
		//查询出所有购物车列表
		List<Cart> selectCartList = cartMapper.selectCartList(cart);
		//把相同的shopId放在同一个分组
		HashSet<Long> hSet = new HashSet<>();
		for (int i = 0; i < selectCartList.size(); i++) {
			Cart cartData = selectCartList.get(i);
			hSet.add(cartData.getShopId());
		}
		List<CartDataInfo> list = new ArrayList<CartDataInfo>();//分组后的list数据
		for (Long shopId : hSet) {
			CartDataInfo cartDataInfo = new CartDataInfo();
			cartDataInfo.setShopId(shopId);
			
			//临时的数组
			List<Cart> listTmp = new ArrayList<Cart>();
			for (int i = 0; i < selectCartList.size(); i++) {
				
				Cart cartDataTmp = selectCartList.get(i);
				if (cartDataTmp.getShopId().equals(shopId)) {
					listTmp.add(cartDataTmp);
				}
			}
			cartDataInfo.setListData(listTmp);
			Cart cartIndex0 = listTmp.get(0);
			cartDataInfo.setShopName(cartIndex0.getShopName());
			list.add(cartDataInfo);
		}
		
		return Result.success(list);
	}

	@Override
	public Result deleteGoodsListOnCart(String token, List<Long> carts) {
		cartMapper.deleteGoodsListOnCart(carts);
		return Result.ok();
	}
}
